<!--
 * @Description: 
 * @Author: charles
 * @Date: 2021-11-11 09:36:52
 * @LastEditors: charles
 * @LastEditTime: 2021-11-11 10:07:43
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>入口</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
  <script src="https://cdn.bootcss.com/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
  <div id="app"></div>

  <script type="text/babel">

    class Modal extends React.Component {
      constructor(props){
        super(props)
        this.el = document.createElement('div')
      }
      componentDidMount(){
        document.body.appendChild(this.el);
      }

      render(){
        return ReactDOM.createPortal(
          this.props.children,
          this.el
        )
      }
    }

    // 异常边界组件 - 用于捕获异常，并且可以将异常信息传递，可以在异常的时候显示提示信息
    class ErrorBoundary extends React.Component {
      constructor(props){
        super(props);
        this.state  = {
          isError:false
        }
      }
      static getDerivedStateFromError(error){
        return {isError: true}
      }
      componentDidCatch(error){
        // 上报异常
        console.log(error);
      }
      
      render(){
        let content ;
        if(this.state.isError){
          content = <div>系统页面异常，请联系管理员：licy@briup.com</div>
        } else {
          content = this.props.children;
        }
        return content;
      }
    }
  
    class App extends React.Component {
      render(){
        return (
            <div className="container">
              <h2>Protals</h2>
              <Modal>
                <div>
                  <h2>模态框</h2>  
                  {

                    Math.random()>0.5?<List/>:<div>hello 一切正常</div>
                  }
                </div>  
              </Modal>
            </div>
        )
      }
    }
    ReactDOM.render(<ErrorBoundary><App/></ErrorBoundary>,document.getElementById('app'))
  </script>
</body>
</html>
